package com.donglink.test.logmonitor.bolt;

import com.donglink.test.logmonitor.entry.Message;
import com.donglink.test.logmonitor.utils.MailUtil;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.IBasicBolt;
import org.apache.storm.topology.IRichBolt;
import org.apache.storm.topology.IWindowedBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Tuple;

import java.util.List;
import java.util.Map;

public class MessageBolt extends BaseRichBolt {
    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {

    }

    @Override
    public void execute(Tuple tuple) {
        List<Message> messages=(List<Message>) tuple.getValue(0);

        for (Message message:messages)
        {
            try {
                MailUtil.sendMail("触发规则",message.getLog(),message.getMail());
            } catch (Exception e) {
                System.out.println("发送邮件失败");
                e.printStackTrace();
            }
        }

    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {

    }
}
